package com.cn.bean;

import java.util.Collection;

import com.cn.xc.utils.CollectionUtil;
import com.cn.xc.utils.ReflectUtil;
import com.cn.xc.utils.StringUtil;

/**
 * FUN: list common capacity
 * 
 * @author sushy
 * @date 2016-3-25
 */
public class XmlSimpleList<T extends SimpleBean> extends SimpleBeanList<T> {
	private static final long serialVersionUID = 543534543492L;

	private Class<T> xmlBeanCls;// list容器装载XmlBean类

	public XmlSimpleList() {
		this(null);
	}

	/**
	 * @param xmlBeanCls
	 *            list容器装载XmlBean类
	 */
	public XmlSimpleList(Class<T> xmlBeanCls) {
		super();
		this.xmlBeanCls = xmlBeanCls;
	}

	public String getXmlBeanCls() {
		if (xmlBeanCls == null) {
			return StringUtil.EMPTY;
		}
		return xmlBeanCls.getName();
	}

	public void setXmlBeanCls(Class<T> xmlBeanCls) {
		this.xmlBeanCls = xmlBeanCls;
	}

	@Override
	public XmlCapacity newInstance() {
		return new XmlSimpleList<T>(xmlBeanCls);
	}

	@Override
	public SimpleBean newEleInstance() {
		return (SimpleBean) ReflectUtil.newInstance(xmlBeanCls);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addFirst(T e) {
		if (e == null) {
			return;
		}

		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		super.addFirst(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addLast(T e) {
		if (e == null) {
			return;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		super.addLast(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean add(T e) {
		if (e == null) {
			return false;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.add(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean addAll(Collection<? extends T> c) {
		if (xmlBeanCls == null) {
			T e = CollectionUtil.getFirst(c);
			if (e == null) {
				return false;
			}
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.addAll(c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean addAll(int index, Collection<? extends T> c) {
		if (xmlBeanCls == null) {
			T e = CollectionUtil.getFirst(c);
			if (e == null) {
				return false;
			}
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.addAll(index, c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T set(int index, T element) {
		if (element == null) {
			return element;
		}

		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) element.getClass();
		}
		return super.set(index, element);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void add(int index, T element) {
		if (element == null) {
			return;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) element.getClass();
		}
		super.add(index, element);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean offer(T e) {
		if (e == null) {
			return false;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.offer(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean offerFirst(T e) {
		if (e == null) {
			return false;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.offerFirst(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean offerLast(T e) {
		if (e == null) {
			return false;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		return super.offerLast(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void push(T e) {
		if (e == null) {
			return;
		}
		if (xmlBeanCls == null) {
			xmlBeanCls = (Class<T>) e.getClass();
		}
		super.push(e);
	}

}
